import {
  Body,
  Controller,
  Delete,
  Get,
  Post,
  Put,
  Query,
} from "@nestjs/common";
import { ApiOperation, ApiTags } from "@nestjs/swagger";
import { APIResponse } from "src/common/decorators/APIResponse.decorators";
import { ArticleService } from "./article.service";
import {
  ArticleTodo,
  CreateArticleTodo,
  UpdateArticleTodo,
} from "./dto/article.dto";

@ApiTags("文章管理")
@Controller("article")
export class ArticleController {
  constructor(private readonly ArticleService: ArticleService) {}

  @Get("list")
  @ApiOperation({ summary: "文章列表" })
  @APIResponse([ArticleTodo], true)
  getAll(@Query("page") page?: number, @Query("userId") userId?: string) {
    return this.ArticleService.findAll(userId);
  }

  @Post("create")
  @ApiOperation({ summary: "新增文章" })
  @APIResponse(true)
  async create(@Body() CreateArticleTodo: CreateArticleTodo): Promise<Boolean> {
    return await this.ArticleService.create(CreateArticleTodo);
  }

  @Put("edit")
  @ApiOperation({ summary: "编辑文章" })
  @APIResponse(true)
  async update(@Body() UserTodo: UpdateArticleTodo): Promise<Boolean> {
    return await this.ArticleService.update(UserTodo.id, UserTodo);
  }

  @Get("info")
  @ApiOperation({ summary: "文章详情" })
  @APIResponse(ArticleTodo)
  async getOne(@Query("id") id: string): Promise<ArticleTodo> {
    // throw new HttpException("请求失败", 500);
    return await this.ArticleService.getOne(id);
  }

  @Delete("delete")
  @ApiOperation({ summary: "删除文章" })
  @APIResponse(true)
  async delete(@Query("id") id: string): Promise<Boolean> {
    return await this.ArticleService.delete(id);
  }
}
